package org.madbit.pingu.adapter;

import java.util.List;

import org.madbit.pingu.R;
import org.madbit.pingu.common.ConnectionState;
import org.madbit.pingu.entity.Connection;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ConnectionsAdapter extends ArrayAdapter<Connection>{
	
	private final Context context;
	private final List<Connection> connections;
	int resource;

	public ConnectionsAdapter(Context context, int resource, List<Connection> connections) {
		super(context, resource, connections);
		this.resource = resource;
		this.connections = connections;
		this.context = context;
	}	
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
		LinearLayout connectionView;
        //Get the current alert object
        Connection conn = getItem(position);        
        
        //Inflate the view
        if(convertView==null)
        {
            connectionView = new LinearLayout(getContext());
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater vi;
            vi = (LayoutInflater)getContext().getSystemService(inflater);
            vi.inflate(resource, connectionView, true);
        }
        else
        {
            connectionView = (LinearLayout) convertView;
        }
        //Get the text boxes from the listitem.xml file
        TextView connectionName =(TextView)connectionView.findViewById(R.id.connectionName);
        TextView connectionIp =(TextView)connectionView.findViewById(R.id.connectionIP);
        ImageView imageView = (ImageView) connectionView.findViewById(R.id.icon);
 
        //Assign the appropriate data from our alert object above
        connectionName.setText(conn.getName());
        connectionIp.setText(conn.getIp());
        
        // Change the icon for Windows and iPhone		
		if (conn.getState() == ConnectionState.OK ) {
			imageView.setImageResource(R.drawable.icon);
		} else if (conn.getState() == ConnectionState.NOK) {
			imageView.setImageResource(R.drawable.icon);
		}
 
        return connectionView;
    }
}
